package com.mnsq.permission.controller;

import com.mnsq.common.bean.annotation.UserInfoRequired;
import com.mnsq.common.bean.entity.permission.Role;
import com.mnsq.common.controller.BaseController;
import com.mnsq.common.service.BaseService;
import com.mnsq.permission.bean.condition.RoleCondition;
import com.mnsq.permission.service.MenuService;
import matrix.module.common.bean.Result;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 周俭
 */
@RestController
@RequestMapping("/api/sys/role/")
public class RoleController extends BaseController<Role, BaseService<Role, RoleCondition>, RoleCondition> {

    @Autowired
    private MenuService menuService;

    /**
     * 查询角色菜单
     *
     * @param roleId
     * @return
     */
    @GetMapping("/menus")
    public Result roleMenus(@RequestParam("roleId") Long roleId) {
        if (roleId == null) {
            return Result.success(null);
        }
        return Result.success(menuService.queryByRoleId(roleId));
    }

    /**
     * 设置角色菜单
     *
     * @param mp roleId,ids
     * @return
     */
    @PostMapping("/roleMenusSet")
    @UserInfoRequired(required = true)
    public Result roleMenusSet(@RequestBody Map<String, String> mp) {
        try {
            Assert.notNull(mp, "参数不能为空");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        menuService.roleMenusSet(MapUtils.getLong(mp, "roleId"), MapUtils.getString(mp, "ids"));
        return Result.success(null);
    }
}
